package org.demo.algorithm.leetcode.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class InorderTraversal {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> st = new Stack<>();
        TreeNode curr = root;
        while (curr != null) {
            st.push(curr);
            if (curr.left != null) {
                curr = curr.left;
            } else if (curr.right != null) {
                curr = curr.right;
            } else {
                TreeNode pop = st.pop();
                result.add(pop.val);
            }
        }
        return result;
    }

}
